package com.hnichr.ump.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hnichr.ump.business.Constant;
import com.hnichr.ump.common.doc.entity.Banner;
import com.hnichr.ump.common.doc.service.BannerService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/doc/banner/v1/")
@Tag(name = "BannerController", description = "文印商城轮播图接口")
public class BannerController {
	@Autowired
	private BannerService bannerService;

	@GetMapping("list")
	@Operation(summary = "轮播图列表", description = "获取轮播图列表（无分页）", method = "GET")
	public Result<List<Banner>> list(@Parameter(description = "查询参数") Criteria criteria) {
		criteria.addCriterion("validFlag", Operator.EQ, Constant.STATUS_VALID);
		criteria.addOrder(new com.mxpioframework.jpa.query.Order("seqNo", true));
		List<Banner> list = bannerService.list(Banner.class, criteria);
		return Result.OK(list);
	}

	@GetMapping("page")
	@Operation(summary = "轮播图列表", description = "获取轮播图列表", method = "GET")
	public Result<Page<Banner>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("validFlag", Operator.EQ, Constant.STATUS_VALID);
		criteria.addOrder(new com.mxpioframework.jpa.query.Order("seqNo", true));
		Page<Banner> list = bannerService.listPage(Banner.class, page, criteria);
		return Result.OK(list);
	}

	@PostMapping("add")
	@Operation(summary = "新增轮播图", description = "新增轮播图", method = "POST")
	public Result<Banner> add(@RequestBody Banner banner) {
		try {
			bannerService.save(banner);
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
		return Result.OK(banner);
	}

	@PutMapping("edit")
	@Operation(summary = "编辑轮播图", description = "编辑轮播图", method = "PUT")
	public Result<Banner> edit(@RequestBody Banner banner) {
		try {
			bannerService.update(banner);
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
		return Result.OK(banner);
	}

	@DeleteMapping("remove/{id}")
	@Operation(summary = "根据id删除轮播图", description = "根据id删除轮播图", method = "DELETE")
	public Result<Banner> remove(@PathVariable(name = "id", required = true) String id) {
		bannerService.delete(Banner.class, id);
		return Result.OK();
	}
}
